package com.bor.gb.core.services;

import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.charset.Charset;

/**
 * Created by bor on 11/27/14.
 */
@Component
public class GooglePlaceService {


    @Value("${google.details.search.url}")
    private String googleSearchDetailsUrl;

    public JSONObject findPlaceById(String id) throws IOException, JSONException {
        String url = String.format(googleSearchDetailsUrl, id);

        InputStream is = new URL(url).openStream();

        BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));

        StringBuilder sb = new StringBuilder();
        int cp;
        while ((cp = rd.read()) != -1) {
            sb.append((char) cp);
        }

        JSONObject json = new JSONObject(sb.toString());
        return json;
    }

  /*

    public GooglePlaceDetails parse(JSONObject json){

        json = json.getJSONObject("result");

        String placeName = json.getString("name");
        JSONArray addresses = json.getJSONArray("address_components");
        for(int i = 0; i < addresses.length(); i++) {
            JSONObject address = addresses.getJSONObject(i);
            if(address.getString(""))
        }
        addresses.forea
        String placeName = json.getString("name");
    }*/
}



